package com.happy_hao.topbiz.util;

import com.happy_hao.topbiz.api.UserApi;
import com.happy_hao.topbiz.exception.ServiceException;
import com.happy_hao.topbiz.po.Group;
import com.happy_hao.topbiz.po.Permission;
import com.happy_hao.topbiz.po.User;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Component
public class RealmUtil {

    @Resource private UserApi userApi;

    public User getUserByMap(String identifier) {
        LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) userApi.readUserByName(identifier).getData().get("user");
        if (map == null) {
            return null;
        }
        User user = new User();
        user.setUserId((Long) map.get("userId"));
        user.setUserName((String) map.get("userName"));
        user.setPassword((String) map.get("password"));
        user.setNickname((String) map.get("nickname"));
        user.setAvatar((String) map.get("avatar"));
        user.setEmail((String) map.get("email"));
        user.setGender((String) map.get("gender"));
        user.setPhone((String) map.get("phone"));
        user.setEnabled((Boolean) map.get("enabled"));
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            user.setCreateAt(formatter.parse((String) map.get("createAt")));
        } catch (
                ParseException e) {
            throw new ServiceException(e.getMessage());
        }
        try {
            user.setUpdateAt(formatter.parse((String) map.get("updateAt")));
        } catch (ParseException e) {
            throw new ServiceException(e.getMessage());
        }
        user.setDescription((String) map.get("description"));
        user.setExtension((String) map.get("extension"));
        List<LinkedHashMap<String, Object>> groups = (List<LinkedHashMap<String, Object>>) map.get("groups");
        List<Group> groupList = new ArrayList<>();
        for (LinkedHashMap<String, Object> group : groups) {
            Group group1 = new Group();
            group1.setGroupId((Long) group.get("groupId"));
            group1.setGroupName((String) group.get("groupName"));
            try {
                group1.setCreateAt(formatter.parse((String) group.get("createAt")));
            } catch (
                    ParseException e) {
                throw new ServiceException(e.getMessage());
            }
            try {
                group1.setUpdateAt(formatter.parse((String) group.get("updateAt")));
            } catch (ParseException e) {
                throw new ServiceException(e.getMessage());
            }
            groupList.add(group1);
        }
        List<LinkedHashMap<String, Object>> permissions = (List<LinkedHashMap<String, Object>>) map.get("permissions");
        List<Permission> permissionList = new ArrayList<>();
        for (LinkedHashMap<String, Object> permission : permissions) {
            Permission permission1 = new Permission();
            permission1.setPermissionId((Long) permission.get("permissionId"));
            permission1.setPermissionName((String) permission.get("permissionName"));
            try {
                permission1.setCreateAt(formatter.parse((String) permission.get("createAt")));
            } catch (
                    ParseException e) {
                throw new ServiceException(e.getMessage());
            }
            try {
                permission1.setUpdateAt(formatter.parse((String) permission.get("updateAt")));
            } catch (ParseException e) {
                throw new ServiceException(e.getMessage());
            }
            permissionList.add(permission1);
        }
        user.setGroups(groupList);
        user.setPermissions(permissionList);
        return user;
    }
}
